package com.wnxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wnxy.dto.NavDto;
import com.wnxy.model.Navigation;
import com.wnxy.mapper.NavigationMapper;
import com.wnxy.service.NavigationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.util.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author alice
 * @since 2021-11-12
 */
@Service
public class NavigationServiceImpl extends ServiceImpl<NavigationMapper, Navigation> implements NavigationService {
    @Autowired
    private RedisTemplate<String, Object> rt;
    private  List<NavDto>  getNav(List<Navigation> navigations){
        Map<Integer, NavDto> navDtoMap = new HashMap<>();
//        for (Navigation navigation : navigations) {
//            NavDto navDto = BeanCopyUtil.copyProperties(navigation, NavDto.class);
//            if(navDto.getNavPid()==0){
//                navDtoMap.put(navDto.getNavId(),navDto);
//            } else {
//                NavDto dto = navDtoMap.get(navDto.getNavPid());
//                if (dto.getChildren()==null) dto.setChildren(new ArrayList<NavDto>());
//                dto.getChildren().add(navDto);
//            }
//        }
        for (Navigation navigation : navigations) {
            NavDto navDto = BeanCopyUtil.copyProperties(navigation, NavDto.class);
            navDtoMap.put(navDto.getNavId(),navDto);
        }
        for (NavDto value : navDtoMap.values()) {
            NavDto np = navDtoMap.get(value.getNavPid());
            if(np!=null){
                if (np.getChildren()==null) np.setChildren(new ArrayList<NavDto>());
                np.getChildren().add(value);
            }
        }
        System.out.println(navDtoMap.values());
        List<NavDto> navDtos =navDtoMap.values().stream().filter(navDto -> (navDto.getChildren() != null||navDto.getNavPid()==0)&&!navDtoMap.containsKey(navDto.getNavPid())).collect(Collectors.toList());
        return navDtos;
    }

    @Override
    public List<NavDto> findNav() {
        List<Navigation> navigations = list(new QueryWrapper<Navigation>().orderByAsc("nav_pid"));
        return getNav(navigations);
    }

    @Override
    public List<NavDto> findNavByManager(Integer managerId) {
        List<Navigation> navigations = getBaseMapper().findAccountMenu(managerId);

        return getNav(navigations);
    }
    @Override
    public List<Integer> findNavByRole(Integer roleId) {
        List<Navigation> navigations = getBaseMapper().findNavByRole(roleId);
        Set<Integer> set = new HashSet<>();
        for (Navigation navigation : navigations) {
            set.add(navigation.getNavPid());
            set.remove(0);
        }
        List<Integer> list = navigations.stream().filter(s -> !set.contains(s.getNavId())).map(nav -> nav.getNavId()).collect(Collectors.toList());
        return list;
    }
    @Override
    public void findNavByRoleRedis() {
        List<Navigation> navigations = getBaseMapper().findNavByRoleRedis();
        rt.opsForValue().set("wnxy:theater:role",navigations);
        Set<Integer> set = new HashSet<>();
        for (Navigation navigation : navigations) {
            set.add(navigation.getNavPid());
            set.remove(0);
        }
        List<Integer> list = navigations.stream().filter(s -> !set.contains(s.getNavId())).map(nav -> nav.getNavId()).collect(Collectors.toList());
    }

}
